// pages/personcenter/personcenter.js
import {ApiRootUrl,apiList} from '../../config/index.js'
import Toast from '../../@vant/weapp/toast/toast';
import {IMG_PATH} from '../../config/index.js'
import {request} from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gonghaoFlag:true,
    gonghao:'暂无',
    bumen:'暂无',
    zhishu:'暂无',
    tishiShow:false,
    statusBarHeight: '',
    activeNames:"",
    activeNames1:"",
    baseinfo:{
     
    },
    companyinfo:{
     
    },
    susheinfo:{
      
    },
    tabList:[{
      title:'基本信息',
    },{
      title:'工作信息',
    },{
      title:'居住信息',
    }],
    active:0
  },
  editClick(){
    if(this.data.gonghao == '暂无'){
      this.setData({
        gonghao:''
      })
    }
    if(this.data.bumen == '暂无'){
      this.setData({
        bumen:''
      })
    }
    if(this.data.zhishu == '暂无'){
      this.setData({
        zhishu:''
      })
    }
    this.setData({
      gonghaoFlag:false
    })
  },
  saveClick(){
    let self = this;
    let openId = wx.getStorageSync("openId");
    let params = {
      openId:openId,
      jobNumber:self.data.gonghao,
      department:self.data.bumen,
      leader:self.data.zhishu,
    }
    let url = `${ApiRootUrl}/labour/editEmployDetail`;
      Toast.loading({
        message: '加载中...',
        mask:true,
        duration: 0,
        forbidClick: true,
      });
      wx.request({
        url:  url,
        method: 'POST',
        header:{
          'content-type': 'application/json; charset=utf-8',
          'cookie': wx.getStorageSync("sessionid")
        },
        params:params,
        data:params,
        success(res) {
          Toast.clear();
          if(res.data.code == 20000){
            self.setData({
              gonghaoFlag:true
            })
          }else{
            Toast({
              duration:3000,
              forbidClick:true,
              type: 'fail',
              message: res.data.message,
            });
          }
        },
        fail(res) {
          Toast.clear();
          Toast({
            duration:3000,
            forbidClick:true,
            type: 'fail',
            message: '网络已断开',
          });
        }
      })
    

  },
  backClick(){
    wx.navigateBack();
  },
  onChange(event){
    this.setData({
      active:event.detail.index
    })
  },
  checkSessionMethod(){
    let self = this;
    let flag = true;
    wx.checkSession({
      success:function(res){
        console.log('session_key未过期')
        //session_key未过期
        flag = true;
      },
      fail:function(res){
        flag = false;
        self.setData({
          tishiShow:true
        })
      }
    })
    if(!flag){
      return;
    }
  },
  initDataList(){
    let self = this;
    self.checkSessionMethod();
    let openId = wx.getStorageSync("openId");
      let url = `${ApiRootUrl}/labour/detailsEmployee/${openId}/3`;
      Toast.loading({
        message: '加载中...',
        mask:true,
        duration: 0,
        forbidClick: true,
      });
      wx.request({
        url:  url,
        method: 'GET',
        header:{
          'content-type': 'application/json; charset=utf-8',
          'cookie': wx.getStorageSync("sessionid")
        },
        success(res) {
          Toast.clear();
          if(res.data.code == 20000){
            let employeeInfo = res.data.data.employeeInfo || {};
            let employeeDetails = res.data.data.employeeDetails || {};
            let employeeDorm = res.data.data.employeeDetails || {};
            let baseinfo = {
              username:employeeInfo.name,
              sex:employeeDetails.sexDesc,
              phone:employeeInfo.phone,
              email:employeeDetails.email,
              identify:employeeInfo.identity,
              minzu:employeeDetails.nation,
              birthday:employeeDetails.birthday,
              hunyin:employeeDetails.isMarryDesc,
              jiguan:employeeDetails.nativePlace
            }
            let companyinfo = {
              name:employeeInfo.labourName,
              position:'无',
              time:employeeInfo.jobTime || '无'
            }
            let susheinfo = {
              position:employeeDorm.dormAddr || '无',
              sushename:employeeDorm.buildNum || '无',
              room:employeeDorm.roomNum || '无',
              chuanwei:employeeDorm.bedNum || '无',
              // suguanname:' 李四',
              // suguanphone:'12939393939',
              time:employeeDorm.entryDorm || '无'
            }
            console.log("===>",res)
            self.setData({
              baseinfo:baseinfo,
              companyinfo:res.data.data.workList,
              inWorkWithHr:res.data.data.inWorkWithHr == null ? []:res.data.data.inWorkWithHr.hrList,
              susheinfo:res.data.data.dormWithManager,
              dormManagerList:res.data.data.dormWithManager == null ? []:res.data.data.dormWithManager.dormManagerList,
            })
          }else{
            Toast({
              duration:3000,
              forbidClick:true,
              type: 'fail',
              message: res.data.message,
            });
          }
        },
        fail(res) {
          Toast.clear();
          Toast({
            duration:3000,
            forbidClick:true,
            type: 'fail',
            message: '网络已断开',
          });
        }
      })
  },
  onChange1(event){
    this.setData({
      activeNames: event.detail
    })
  },
  onChange2(event){
    this.setData({
      activeNames1: event.detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusBarHeight:wx.getSystemInfoSync()['statusBarHeight']
    });
    // this.initDataList();
    this.loadInfo()
  },
  loadInfo(){
    let openId = wx.getStorageSync('openId')
    Toast.loading({
      message: '加载中...',
      mask:true,
      duration: 0,
      forbidClick: true,
    });
    request(apiList.detailSelfInfo+openId,{},"GET").then(res =>{
      Toast.clear()
      console.log(res)
      if(res.data){
        this.setData({
          baseinfo:res.data.employeeSelfBase||{},
          companyinfo:res.data.employeeSelfWork||{},
          susheinfo:res.data.employeeSelfDorm||{},
          dormManagerList:res.data.employeeSelfDorm == null ?[]: res.data.employeeSelfDorm.dormList,
          inWorkWithHr:res.data.employeeSelfWork.hrList == null ? []:res.data.employeeSelfWork.hrList,
          gonghao:res.data.employeeSelfWork.jobNumber || '暂无',
          bumen:res.data.employeeSelfWork.department || '暂无',
          zhishu:res.data.employeeSelfWork.leader || '暂无',
        })
        console.log(this.data.companyinfo)
      }
      
    }).catch(err =>{
      Toast.clear()
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
      });
    })
  }, 
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})